package com.atguigu.eud_teacher.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eud_teacher.entity.CourseEntity;
import com.atguigu.eud_teacher.entity.EduTeacherEntity;
import com.atguigu.eud_teacher.service.CourseService;
import com.atguigu.eud_teacher.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: 2021-04-18 10:04
 */
@CrossOrigin
@RestController
@Api(tags = "--前台讲师信息API")
@RequestMapping("/vue-admin-template/teacher")
public class TeacherFrontController {

    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private CourseService courseService;

    @ApiOperation(value="获取讲师分页")
    @GetMapping("/getFrontTeacherList/{page}/{limit}")
    public R getFrontTeacherList(@PathVariable long page,
                                 @PathVariable long limit){
        Page<EduTeacherEntity> pageTeacher=new Page<>(page,limit);
        IPage<EduTeacherEntity> list = eduTeacherService.page(pageTeacher, new QueryWrapper<EduTeacherEntity>().orderByDesc("gmt_create"));
        long total = list.getTotal();
        List<EduTeacherEntity> records = list.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);
    }

    @ApiOperation(value="根据id获取讲师详情")
    @GetMapping("/getTeacherViewById/{id}")
    public R getTeacherViewById(@PathVariable String id){
        EduTeacherEntity eduTeacherEntity=eduTeacherService.getById(id);
        List<CourseEntity> courseEntityList=courseService.list(new QueryWrapper<CourseEntity>().eq("teacher_id",id));
        return R.ok().data("eduTeacherEntity",eduTeacherEntity).data("courseEntityList",courseEntityList);
    }
}
